﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

/// <summary>
/// YCSteps 异常步骤相关类
/// </summary>
public class YCSteps
{
    public string YCID;
    public int SEQ;
    public string OperatorID;
    public string ToUserName;
    public string Annotate;
    public DateTime BeginDate;
    public DateTime EndDate;
    public string Status;
    public int DepID;
    private int _AutoID;
	public YCSteps()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//

	}
    /// <summary>
    /// 根据步骤编号和异常编号构造一个实例
    /// </summary>
    /// <param name="autoID">步骤编号</param>
    /// <param name="tempYCID">异常编号</param>
    public YCSteps(int autoID,string tempYCID)
    {
        string theYear = tempYCID.Substring(2, 4);
        string tableName = "YCSTEPS" + theYear;
        string sqlString = "select * from " + tableName + " where AUTOID=" + autoID + "";
        DBOperation DBO = new DBOperation();
        SqlDataReader sdr = DBO.GetDataReader(sqlString);
        if (sdr.Read())
        {
            this._AutoID = Convert.ToInt32(sdr["AUTOID"].ToString());
            this.YCID = sdr["YCID"].ToString();
            this.SEQ = Convert.ToInt32(sdr["SEQ"].ToString());
            this.OperatorID = sdr["OPERATORID"].ToString();
            this.ToUserName = sdr["TOUSERNAME"].ToString();
            this.Annotate = sdr["ANNOTATE"].ToString();
            this.BeginDate = Convert.ToDateTime(sdr["BEGINDATE"].ToString());
            if (sdr["ENDDATE"].ToString() != "")
            {
                this.EndDate = Convert.ToDateTime(sdr["ENDDATE"].ToString());
 
            }
            this.Status = sdr["STATUS"].ToString();
            this.DepID = Convert.ToInt32(sdr["DEPID"].ToString());
        }
        sdr.Close();

    }

    /// <summary>
    /// 添加异常处理步骤
    /// </summary>
    /// <returns>添加成功则返回true，否则返回false</returns>
    public Boolean AddYCSteps()
    {
        Boolean success = false;
        //设置跟上一步有关的信息
        SetPreStepMsg();
        BasicClass BC = new BasicClass();
        this.DepID = Convert.ToInt32(BC.GetDepIDByUserName(this.OperatorID));
        string dataYear = this.YCID.Substring(2, 4);
        string tableName = "YCSTEPS" + dataYear;
        string sqlString = "insert into " + tableName + " (YCID,SEQ,OPERATORID,TOUSERNAME,ANNOTATE,STATUS,BEGINDATE,ENDDATE,DEPID) values ('";
        sqlString += this.YCID + "'," + this.SEQ + ",'" + this.OperatorID + "','" + this.ToUserName + "','" + this.Annotate + "'," + this.Status + ",'" + this.BeginDate + "','" + this.EndDate + "'," + this.DepID + ")";
        DBOperation DBO = new DBOperation();
        if (DBO.ExcuteSql(sqlString))
        {  
            success = true;

        }
        else
        {
            success = false;
        }
        return success; 
    }
    /// <summary>
    /// 设置跟上一步有关的信息，SEQ，BEGINDATE
    /// </summary>
    protected void SetPreStepMsg()
    {
        string dataYear = this.YCID.Substring(2, 4);
        string tableName = "YCSTEPS" + dataYear;
        string sqlString = "select top 1 * from " + tableName + " where YCID='" + this.YCID + "' order by SEQ desc,AUTOID desc";
        DBOperation DBO = new DBOperation();
        SqlDataReader sdr = DBO.GetDataReader(sqlString);
        if (sdr.Read())
        {
            this.SEQ = Convert.ToInt32(sdr["SEQ"].ToString()) + 1;
            this.BeginDate = Convert.ToDateTime(sdr["ENDDATE"].ToString());
        }
        else
        {
            this.SEQ = 1;
            this.BeginDate = DateTime.Now;
        }
        sdr.Close();

    }
    /// <summary>
    /// 更新当前操作者
    /// </summary>
    /// <returns></returns>
    public Boolean UpdateCurrentOperater()
    {
        Boolean success = false;
        string dataYear = this.YCID.Substring(2, 4);
        string tableName = "YCMS" + dataYear;
        string sqlString = "update " + tableName + " set CURRENTOPERATER='" + this.ToUserName + "' where YCID='"+this.YCID+"'";
        DBOperation DBO = new DBOperation();
        if (DBO.ExcuteSql(sqlString))
        {
            success = true;
        }
        else
        {
            success = false;
        }
        return success; 
    }
    /// <summary>
    /// 修改异常处理状态
    /// </summary>
    /// <returns></returns>
    public Boolean UpdateYCStatus()
    {
        Boolean success = false;
        string dataYear=this.YCID.Substring(2,4);
        string tableName="YCMS"+dataYear;
        string sqlString = "update " + tableName + " set ENDDATE='" + this.EndDate + "',STATUS='1',RESULT='" + this.Annotate + "' where YCID='"+this.YCID+"'";
        DBOperation DBO = new DBOperation();
        if (DBO.ExcuteSql(sqlString))
        {
            success = true;
        }
        else
        {
            success = false;
        }
        return success;
 
    }
    /// <summary>
    /// 向下一异常处理人发送信息
    /// </summary>
    /// <returns></returns>
    public Boolean SendMsgToNextUser()
    {
        string title = "您有新的线损异常需要处理！";
        string YCtitle = BasicClass.GetYCTitleByYCID(this.YCID);
        string addcontent = "您有新的线损异常需要处理了！异常号:" + this.YCID + "，名称:"+YCtitle+"。批注：" + this.Annotate;
        messages sendMS = new messages();
        sendMS.FromUserName = this.OperatorID;
        sendMS.ToUserName = this.ToUserName;
        sendMS.TrueFlowID = this.YCID;
        sendMS.Title = title;
        sendMS.Content = addcontent;
        if (sendMS.SendCheckDataMessage())
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    /// <summary>
    /// 根据异常ID获取该异常的所有审核步骤信息
    /// </summary>
    /// <param name="tempYCID">异常ID</param>
    /// <returns>返回记录所有步骤信息的DataSet对象</returns>
    public DataSet GetAllStepsByYCID(string tempYCID)
    {
        string theYear = tempYCID.Substring(2,4);
        string tableName = "YCSTEPS" + theYear;
        string sqlString = "select * from " + tableName + " where YCID='" + tempYCID + "' and SEQ<>1 order by SEQ ASC";
        DBOperation DBO = new DBOperation();
        DataSet tempDS = DBO.GetDataSet(sqlString, "YCSteps");
        return tempDS;
    }
    /// <summary>
    /// 更新审核步骤信息
    /// </summary>
    /// <returns></returns>
    public Boolean UpdateYCStep()
    {
        bool success = false;
        string theYear = this.YCID.Substring(2, 4);
        string tableName = "YCSTEPS" + theYear;
        string sqlString = "update " + tableName + " set YCID='" + this.YCID + "',SEQ=" + this.SEQ + ",OPERATORID='" + this.OperatorID + "'";
        sqlString += ",ToUSERNAME='" + this.ToUserName + "',ANNOTATE='" + this.Annotate + "',BEGINDATE='" + this.BeginDate.ToString() + "',ENDDATE='" + this.EndDate + "',STATUS='" + this.Status + "',DEPID=" + this.DepID.ToString() + " where AUTOID="+this._AutoID+"";
        try
        {
            DBOperation DBO = new DBOperation();
            DBO.ExcuteSql(sqlString);
            success = true;
        }
        catch (Exception ex)
        {
            string errorMsg = ex.Message.ToString();
            success = false;
            throw (ex);
        }

        return success;
    }
    
}
